package com.esirong.whatime.presenter;

import android.text.TextUtils;

import com.esirong.dao.Task;
import com.esirong.whatime.model.ResultHandler;
import com.esirong.whatime.model.TaskModel;
import com.esirong.whatime.mvp.BasePresenter;
import com.esirong.whatime.util.Logger;

/**
 * Created by my daling on 2015/7/14.
 */
public class AddTaskPresenter extends BasePresenter<IAddTaskView> {

    public void saveNewTask(Task task){
        if(checkInput(task)){
            saveTask(task);
        }

    }

    public boolean checkInput(Task task) {
        if(TextUtils.isEmpty(task.getTitle())){
            //标题空
            getView().showMessage("标题为空");
            return false;
        }
        if(TextUtils.isEmpty(task.getType())){
            //类型空
            getView().showMessage("类型为空");
            return false;
        }
        if(TextUtils.isEmpty(task.getAddress())){
            //类型空
            getView().showMessage("地址为空");
            return false;
        }
        //其他默认值
        return true;
    }

    private void saveTask(Task taskInfo){

        TaskModel.addTask(null, taskInfo, new ResultHandler<Task>() {
            @Override
            public void onSuccess(Task result) {
                //保存成功
                Logger.i("", "保存task成功");
                getView().showMessage("保存task成功");
            }

            @Override
            public boolean onFailed(int code) {
                Logger.i("", "保存task失败");
                getView().showMessage("保存task失败");
                return false;
            }
        });
    }
}
